package com.hmf.contrller;

import com.hmf.bean.*;
import com.hmf.service.HmfListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@ResponseBody  //将方法的返回值，以特定的格式(json格式的数据)写入到response的body区域，进而将数据返回给客户端。
public class HmfListContrller {

    @Autowired
    HmfListService listService;

    //增加单车
    @RequestMapping(value = "/list/addBicycle", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMsg<Integer> addBicycle(@RequestBody Bicycle bicycle) {
        ResponseMsg<Integer> res = new ResponseMsg<>();
        try {
            res.success("成功");
            res.setData(listService.addBicycle(bicycle));
        } catch (Exception e) {
            res.fail("失败");
        }
        return res;
    }

    //删除单车
    @RequestMapping(value = "/list/deleteBicycle", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> deleteBicycle(@RequestBody Map<String, String> request) {
        Map<String, Object> res = new HashMap<>();

        if (request.containsKey("bicycleNumber")) {
            String bicycleNumber = request.get("bicycleNumber");
            try {
                int result = listService.deleteBicycle(bicycleNumber);
                if (result == 1) {
                    res.put("message", "删除成功");
                } else {
                    res.put("message", "删除失败");
                }
            } catch (Exception e) {
                res.put("message", "删除失败，发生错误");
                e.printStackTrace(); // 记录日志或其他错误处理
            }
        } else {
            res.put("message", "请求缺少repairID参数");
        }
        return res;
    }

    //修改单车信息
    @RequestMapping(value = "/list/updateBicycle",method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMsg<Integer> updateBicycle(@RequestBody Bicycle bicycle) {
        ResponseMsg<Integer> res = new ResponseMsg<>();
        try {
            res.success("成功");
            res.setData(listService.updateBicycle(bicycle));
        } catch (Exception e) {
            res.fail("失败");
        }
        return res;
    }

    //根据单车编号查询单车
    @RequestMapping(value = "/list/searchBicycle",method = {RequestMethod.GET, RequestMethod.POST})
    public Bicycle searchBicycle(@RequestBody Map<String, String> requestBody) {
        String bicycleNumber = requestBody.get("bicycleNumber");
        if (bicycleNumber != null) {
            Bicycle bicycle = listService.searchBicycle(bicycleNumber);
            System.out.println(bicycle);
            return bicycle;
        } else {
            // 处理参数错误或缺失的情况
            return null;
        }
    }

    //分页查询
    @RequestMapping("/getBicyclesByPage")
    public List<Bicycle> getBicyclesByPage(@RequestBody Map<String, Object> pages) {
//        Map<String, Object> pages = new HashMap<>();
//        pages.put("start",1);//从位置为1的地方开始
//        pages.put("pageSize",2);//查询两个
        return listService.getBicyclesByPage(pages);
    }


}
